package com.springboot.study.tests.lock;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.LockSupport;

/**
 * @Author: guodong
 * @Date: 2021/6/1 11:14
 * @Version: 1.0
 * @Description:
 */
@Slf4j
public class LockSupportTest {

    private static volatile List<Integer> list = new ArrayList<>();

    static Thread currntThrad = null;

    public static void main(String[] args) {
        currntThrad = new Thread(new A());
        Thread b = new Thread(new B());

    }


    static class A implements Runnable{
        @Override
        public void run() {
            if(list.size() != 5){
                System.out.println(list);
                LockSupport.park();
            }else {
                System.out.println(list);
            }
        }
    }




    static class B implements Runnable{
        @Override
        public void run() {
         for(int i = 1; i < 10; i++){
             list.add(i);
             if(list.size() == 5){
                 LockSupport.unpark(currntThrad);
             }
         }
        }
    }




}
